package com.bosssoft.trainee.nontax.hystrix;

import com.alibaba.fastjson.JSONObject;
import com.bosssoft.trainee.nontax.common.result.Result;
import com.bosssoft.trainee.nontax.entity.dto.TokenResult;
import com.bosssoft.trainee.nontax.entity.dto.UserInfo;
import com.bosssoft.trainee.nontax.entity.vo.RoleVo;
import com.bosssoft.trainee.nontax.entity.vo.UserVo;
import com.bosssoft.trainee.nontax.service.RoleClientService;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Luo Wenlong
 * @date 2022-07-11 20:24
 * @description:
 * @version:
 */
@Component
public class RoleClientServiceFallBackFactory implements FallbackFactory<RoleClientService> {
    @Override
    public RoleClientService create(Throwable throwable) {
        return new RoleClientService() {
            @Override
            public Result<List<RoleVo>> getAllRoles() {
                LinkedList<RoleVo> roleVos = new LinkedList<>();
                roleVos.add(RoleVo.builder()
                        .roleId(-1)
                        .roleName("system is busy")
                        .roleDesc("no use data, system is busy").build());
                return Result.success(roleVos);
            }

            @Override
            public Result<UserVo> getUserById(Integer id) {
                return Result.success(
                        UserVo.builder()
                                .userId(-1)
                                .tel("00000000000")
                                .deptName("系统繁忙")
                                .description("系统繁忙 请稍后再试")
                                .build());
            }

            @Override
            public Result<TokenResult> login(JSONObject jsonObject) {
                return Result.success(TokenResult.builder().token("fall back token, no use!").build());
            }

            @Override
            public Result<UserInfo> getInfo(String token) {
                return Result.success(
                        UserInfo.builder()
                                .name("系统繁忙")
                                .introduction("系统繁忙，请稍后再试")
                                .build());
            }
        };
    }
}
